<template>
	<view class="cu-modal" :class="showModal?'show':''">
		<view class="cu-dialog activate-modal">
			<view class="modal-header">
				<view class="header-bg">
					<view class="gradient-circle"></view>
				</view>
				<view class="close-btn" @tap="closeModal">
					<text class="cuIcon-close"></text>
				</view>
			</view>
			
			<view class="modal-content">
				<view class="icon-container">
					<view class="icon-wrapper">
						<text class="cuIcon-diamond icon-main"></text>
					</view>
					<view class="icon-glow"></view>
				</view>
				
				<view class="title-section">
					<text class="modal-title">{{title}}</text>
					<text class="modal-subtitle">输入激活码解锁专属权益</text>
				</view>
				
				<view class="input-section">
					<view class="input-wrapper">
						<text class="input-icon cuIcon-key"></text>
						<input 
							class="activate-input" 
							placeholder="请输入激活码" 
							v-model="activateCode"
							maxlength="64"
							placeholder-class="input-placeholder"
						/>
					</view>
				</view>
				
				<view class="tips-section">
					<text class="tip-text">激活失败可联系在线客服获取帮助</text>
				</view>
				
				<view class="action-section">
					<button class="action-btn service-btn" open-type="contact" session-from="激活码客服">
						<text class="btn-icon cuIcon-service"></text>
						<text class="btn-text">在线客服</text>
					</button>
					<button class="action-btn activate-btn" @tap="activateVIP" :disabled="!activateCode">
						<text class="btn-text">立即激活</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
let app = getApp();
export default {
	props: {
		// 是否显示弹窗
		show: {
			type: Boolean,
			default: false
		},
		// 弹窗标题
		title: {
			type: String,
			default: '激活题库'
		},
		// 业务ID
		businessId: {
			type: [Number, String],
			default: ''
		},
		// 业务类型
		businessType: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			showModal: false,
			activateCode: ''
		};
	},
	watch: {
		show(newVal) {
			this.showModal = newVal;
		}
	},
	created() {
		this.showModal = this.show;
	},
	methods: {
		// 关闭弹窗
		closeModal() {
			this.showModal = false;
			this.$emit('update:show', false);
		},

		// 激活码验证
		activateVIP() {
			if (!this.activateCode) {
				app.showToast('请输入激活码');
				return;
			}
			
			// 调用激活接口
			app.globalData.server
				.postRequest('activationCode/confirm', {
					code: this.activateCode,
					business_id: this.businessId,
					business_type: this.businessType
				})
				.then(res => {
					app.showToast('激活成功');
					this.closeModal();
					this.activateCode = '';
					// 触发激活成功事件
					this.$emit('success', res.data);
				})
				.catch(err => {
					console.log(err);

					app.showToast(err.data.message || '激活失败');
					// 触发激活失败事件
					this.$emit('fail', err);
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.activate-modal {
	width: 680rpx;
	border-radius: 32rpx;
	overflow: hidden;
	background: linear-gradient(145deg, #ffffff 0%, #f8faff 100%);
	box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.15);
	position: relative;
}

.modal-header {
	position: relative;
	height: 160rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	overflow: hidden;
}

.header-bg {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	overflow: hidden;
}

.gradient-circle {
	position: absolute;
	width: 400rpx;
	height: 400rpx;
	border-radius: 50%;
	background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0.05) 100%);
	top: -200rpx;
	right: -100rpx;
}

.close-btn {
	position: absolute;
	top: 30rpx;
	right: 30rpx;
	width: 60rpx;
	height: 60rpx;
	background: rgba(255,255,255,0.2);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	backdrop-filter: blur(10rpx);
	z-index: 10;
}

.close-btn .cuIcon-close {
	color: #fff;
	font-size: 32rpx;
	font-weight: bold;
}

.modal-content {
	padding: 60rpx 50rpx;
	text-align: center;
}

.icon-container {
	position: relative;
	display: inline-block;
	margin-bottom: 40rpx;
}

.icon-wrapper {
	width: 120rpx;
	height: 120rpx;
	background: linear-gradient(135deg, #ffd89b 0%, #19547b 100%);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	position: relative;
	z-index: 2;
	box-shadow: 0 10rpx 30rpx rgba(102, 126, 234, 0.3);
}

.icon-main {
	font-size: 60rpx;
	color: #fff;
}

.icon-glow {
	position: absolute;
	top: -10rpx;
	left: -10rpx;
	right: -10rpx;
	bottom: -10rpx;
	background: linear-gradient(135deg, rgba(102, 126, 234, 0.2) 0%, rgba(118, 75, 162, 0.2) 100%);
	border-radius: 50%;
	z-index: 1;
	animation: pulse 2s infinite;
}

@keyframes pulse {
	0% { transform: scale(0.95); opacity: 0.7; }
	50% { transform: scale(1.05); opacity: 0.3; }
	100% { transform: scale(0.95); opacity: 0.7; }
}

.title-section {
	margin-bottom: 50rpx;
}

.modal-title {
	display: block;
	font-size: 40rpx;
	font-weight: 700;
	color: #2c3e50;
	margin-bottom: 16rpx;
}

.modal-subtitle {
	display: block;
	font-size: 28rpx;
	color: #7f8c8d;
	font-weight: 400;
}

.input-section {
	margin-bottom: 40rpx;
}

.input-wrapper {
	position: relative;
	display: flex;
	align-items: center;
	background: #f8f9fa;
	border-radius: 50rpx;
	padding: 0 40rpx;
	height: 100rpx;
	border: 2rpx solid #e9ecef;
	transition: all 0.3s ease;
}

.input-wrapper:focus-within {
	border-color: #667eea;
	background: #fff;
	box-shadow: 0 0 0 4rpx rgba(102, 126, 234, 0.1);
}

.input-icon {
	font-size: 36rpx;
	color: #adb5bd;
	margin-right: 20rpx;
}

.activate-input {
	flex: 1;
	height: 100rpx;
	font-size: 32rpx;
	color: #2c3e50;
	background: transparent;
	border: none;
	outline: none;
}

.input-placeholder {
	color: #adb5bd;
	font-size: 30rpx;
}

.tips-section {
	margin-bottom: 50rpx;
}

.tip-text {
	font-size: 24rpx;
	color: #95a5a6;
}

.action-section {
	display: flex;
	gap: 30rpx;
	justify-content: center;
}

.action-btn {
	flex: 1;
	height: 90rpx;
	border-radius: 45rpx;
	border: none;
	font-size: 30rpx;
	font-weight: 600;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 10rpx;
	transition: all 0.3s ease;
	cursor: pointer;
}

.service-btn {
	background: #f8f9fa;
	color: #6c757d;
	border: 2rpx solid #dee2e6;
}

.service-btn:active {
	background: #e9ecef;
	transform: translateY(2rpx);
}

.activate-btn {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	color: #fff;
	box-shadow: 0 8rpx 20rpx rgba(102, 126, 234, 0.3);
}

.activate-btn:active {
	transform: translateY(2rpx);
	box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.4);
}

.activate-btn[disabled] {
	opacity: 0.5;
	transform: none;
	box-shadow: none;
}

.btn-icon {
	font-size: 32rpx;
}

.btn-text {
	font-size: 30rpx;
	font-weight: 600;
}

/* 添加进入动画 */
.cu-modal.show .activate-modal {
	animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
	from {
		transform: scale(0.8);
		opacity: 0;
	}
	to {
		transform: scale(1);
		opacity: 1;
	}
}
</style>